'use strict'

module.exports = (app) => {
	const { STRING, INTEGER, DATE, BOOLEAN } = app.Sequelize

	const Role = app.model.define(
		'roles',
		{
			id: {
				type: INTEGER,
				autoIncrement: true,
				allowNull: true,
				primaryKey: true,
			},
			name: {
				type: STRING(30),
				primaryKey: true,
			},

			remark: STRING,
			description: INTEGER,
			status: {
				type: BOOLEAN,
				defaultValue: true,
			},
			createdAt: DATE,
			updatedAt: DATE,
			deletedAt: DATE,
		},
		{
			tableName: 'roles', //指定表名称
			timestamps: true,
		}
	)
	Role.associate = function () {
		Role.belongsToMany(app.model.User, {
			through: app.model.RoleUser,
			foreignKey: 'roleId',
			otherKey: 'userId',
		})
		Role.belongsToMany(app.model.Menu, {
			through: app.model.RoleMenu,
			foreignKey: 'rid',
			otherKey: 'mid',
		})
	}

	return Role
}
